import datetime
import json
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.shortcuts import render
from First import models
import numpy as np
import pandas as pd
import itertools
from dateutil.parser import parse
from First.DataAccess import utils, zhuanhuafenxi
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import register_job, DjangoJobStore
from First.DataAccess import shijianxinxi, yonghufenxi, shiyongxingwei, qudaoxinxi, liucunfenxi

scheduler = BackgroundScheduler(timezone='Asia/Shanghai')
scheduler.add_jobstore(DjangoJobStore(), "default")  # 在数据库中会添加任务记录
# scheduler.start()  # 启动任务

# 时间间隔5秒钟打印一次当前的时间
@register_job(scheduler, "interval", seconds=230, id='rwshijianfenxi')
def rwshijianfenxi():
    start_date, end_date = utils.fdate()
    str_json = shijianxinxi.shijianfenxi(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Shijianxinxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Shijianxinxi.objects.filter(shijian=parse(end_date)).update(shijianfenxi=str_json)
        else:
            models.Shijianxinxi.objects.create(shijian=parse(end_date), shijianfenxi=str_json)


@register_job(scheduler, "interval", seconds=235, id='rwyonghuqushi')
def rwyonghuqushi():
    start_date, end_date = utils.fdate()
    str_json = yonghufenxi.yonghuqushi(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Yonghufenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Yonghufenxi.objects.filter(shijian=parse(end_date)).update(yonghuqushi=str_json)
        else:
            models.Yonghufenxi.objects.create(shijian=parse(end_date), yonghuqushi=str_json)


@register_job(scheduler, "interval", seconds=250, id='rwhuoyueyonghu')
def rwhuoyueyonghu():
    start_date, end_date = utils.fdate()
    str_json = yonghufenxi.huoyueyonghu(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Yonghufenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Yonghufenxi.objects.filter(shijian=parse(end_date)).update(huoyueyonghu=str_json)
        else:
            models.Yonghufenxi.objects.create(shijian=parse(end_date), huoyueyonghu=str_json)


@register_job(scheduler, "interval", seconds=245, id='rwyonghuhuaxiang')
def rwyonghuhuaxiang():
    start_date, end_date = utils.fdate()
    str_json = yonghufenxi.yonghuhuaxiang(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Yonghufenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Yonghufenxi.objects.filter(shijian=parse(end_date)).update(yonghuhuaxiang=str_json)
        else:
            models.Yonghufenxi.objects.create(shijian=parse(end_date), yonghuhuaxiang=str_json)


@register_job(scheduler, "interval", seconds=240, id='rwdiyufenbu')
def rwdiyufenbu():
    start_date, end_date = utils.fdate()
    str_json = yonghufenxi.diyufenbu(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Yonghufenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Yonghufenxi.objects.filter(shijian=parse(end_date)).update(diyufenbu=str_json)
        else:
            models.Yonghufenxi.objects.create(shijian=parse(end_date), diyufenbu=str_json)


@register_job(scheduler, "interval", seconds=255, id='rwzhongduanfenxi')
def rwzhongduanfenxi():
    start_date, end_date = utils.fdate()
    str_json = yonghufenxi.zhongduanfenxi(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Yonghufenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Yonghufenxi.objects.filter(shijian=parse(end_date)).update(zhongduanfenxi=str_json)
        else:
            models.Yonghufenxi.objects.create(shijian=parse(end_date), zhongduanfenxi=str_json)


@register_job(scheduler, "interval", seconds=260, id='rwbanbenfenxi')
def rwbanbenfenxi():
    start_date, end_date = utils.fdate()
    str_json = yonghufenxi.banbenfenxi(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Yonghufenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Yonghufenxi.objects.filter(shijian=parse(end_date)).update(banbenfenxi=str_json)
        else:
            models.Yonghufenxi.objects.create(shijian=parse(end_date), banbenfenxi=str_json)


@register_job(scheduler, "interval", seconds=265, id='rwyemianfenxi')
def rwyemianfenxi():
    start_date, end_date = utils.fdate()
    str_json = shiyongxingwei.yemianfenxi(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Shiyongxingwei.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Shiyongxingwei.objects.filter(shijian=parse(end_date)).update(yemianfenxi=str_json)
        else:
            models.Shiyongxingwei.objects.create(shijian=parse(end_date), yemianfenxi=str_json)


@register_job(scheduler, "interval", seconds=270, id='rwyemianlujing')
def rwyemianlujing():
    start_date, end_date = utils.fdate()
    str_json = shiyongxingwei.yemianlujing(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Shiyongxingwei.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Shiyongxingwei.objects.filter(shijian=parse(end_date)).update(yemianlujing=str_json)
        else:
            models.Shiyongxingwei.objects.create(shijian=parse(end_date), yemianlujing=str_json)


@register_job(scheduler, "interval", seconds=275, id='rwshiyongxiguan')
def rwshiyongxiguan():
    start_date, end_date = utils.fdate()
    str_json = shiyongxingwei.shiyongxiguan(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Shiyongxingwei.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Shiyongxingwei.objects.filter(shijian=parse(end_date)).update(shiyongxiguan=str_json)
        else:
            models.Shiyongxingwei.objects.create(shijian=parse(end_date), shiyongxiguan=str_json)


@register_job(scheduler, "interval", seconds=280, id='rwqudaofenxi')
def rwqudaofenxi():
    start_date, end_date = utils.fdate()
    str_json = qudaoxinxi.qudaofenxi(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Qudaoxinxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Qudaoxinxi.objects.filter(shijian=parse(end_date)).update(qudaofenxi=str_json)
        else:
            models.Qudaoxinxi.objects.create(shijian=parse(end_date), qudaofenxi=str_json)


@register_job(scheduler, "cron", hour='10', minute='28', id='rwxinyonghushouciliucun')
def rwxinyonghushouciliucun():
    start_date, end_date = utils.zhdate()
    str_json = liucunfenxi.xinyonghushouciliucun(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Liucunfenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Liucunfenxi.objects.filter(shijian=parse(end_date)).update(xinyonghushouciliucun=str_json)
        else:
            models.Liucunfenxi.objects.create(shijian=parse(end_date), xinyonghushouciliucun=str_json)


@register_job(scheduler, "cron", hour='10', minute='29', id='rwxinyonghuzidingyiliucun')
def rwxinyonghuzidingyiliucun():
    start_date, end_date = utils.zhdate()
    str_json = liucunfenxi.xinyonghuzidingyiliucun(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Liucunfenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Liucunfenxi.objects.filter(shijian=parse(end_date)).update(xinyonghuzidingyiliucun=str_json)
        else:
            models.Liucunfenxi.objects.create(shijian=parse(end_date), xinyonghuzidingyiliucun=str_json)


@register_job(scheduler, "cron", hour='10', minute='30', id='rwhuoyueyonghuliucun')
def rwhuoyueyonghuliucun():
    start_date, end_date = utils.zhdate()
    str_json = liucunfenxi.huoyueyonghuliucun(start_date, end_date)
    # print(str_json)
    if str_json != None:
        if (models.Liucunfenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
            models.Liucunfenxi.objects.filter(shijian=parse(end_date)).update(huoyueyonghuliucun=str_json)
        else:
            models.Liucunfenxi.objects.create(shijian=parse(end_date), huoyueyonghuliucun=str_json)


@register_job(scheduler, "interval", seconds=290, id='rwzhuanhualoudou')
def rwzhuanhualoudou():
    start_date, end_date = utils.fdate()
    str_json = zhuanhuafenxi.zhuanhualoudou()
    # print(str_json)
    if (models.Zhuanhuafenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
        models.Zhuanhuafenxi.objects.filter(shijian=parse(end_date)).update(zhuanhualoudou=str_json)
    else:
        models.Zhuanhuafenxi.objects.create(shijian=parse(end_date), zhuanhualoudou=str_json)


@register_job(scheduler, "interval", seconds=285, id='rwlujingzhuanhualoudou')
def rwlujingzhuanhualoudou():
    start_date, end_date = utils.fdate()
    str_json = zhuanhuafenxi.lujingzhuanhualoudou(start_date, end_date)
    # print(str_json)
    if (models.Zhuanhuafenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
        models.Zhuanhuafenxi.objects.filter(shijian=parse(end_date)).update(lujingzhuanhua=str_json)
    else:
        models.Zhuanhuafenxi.objects.create(shijian=parse(end_date), lujingzhuanhua=str_json)


@register_job(scheduler, "interval", seconds=295, id='rwqudaozhuanhualoudou')
def rwqudaozhuanhualoudou():
    start_date, end_date = utils.fdate()
    str_json = zhuanhuafenxi.qudaozhuanhualoudou(start_date, end_date)
    # print(str_json)
    if (models.Zhuanhuafenxi.objects.filter(shijian=parse(end_date)).count() > 0):  # 查询符合条件数据的数量
        models.Zhuanhuafenxi.objects.filter(shijian=parse(end_date)).update(qudaozhuanhua=str_json)
    else:
        models.Zhuanhuafenxi.objects.create(shijian=parse(end_date), qudaozhuanhua=str_json)


@login_required
def getDataByKey(request):
    request.encoding = 'utf-8'
    lists = []
    if 'type' in request.GET and request.GET['type']:
        if request.GET['type'] == 'huoqurenwuid':
            ulist = models.DjangoApschedulerDjangojob.objects.values_list("id", "next_run_time")
            for ob in ulist:
                if (ob[1] == None):
                    lists.append({'id': ob[0], 'next_run_time': "已暂停", 'enable': '0'})
                else:
                    lists.append({'id': ob[0], 'next_run_time': ob[1].strftime('%Y-%m-%d %H:%M:%S'), 'enable': '1'})
        elif request.GET['type'] == 'atzhongduanfenxi':
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            qleiming = request.GET['leiming']
            ulist = models.Yonghufenxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                         "zhongduanfenxi")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and (qleiming in ob[1]):
                        for x in range(len(ob[1]['zhongduanming'])):
                            lists1.append({'name': ob[1]['zhongduanming'][x], 'value': ob[1][qleiming][x],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            # print(df1)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['name'], as_index=False).agg({'value': 'mean'})
            lists2 = group_name1.to_dict(orient='list')
            for x2 in range(len(lists2['name'])):
                lists.append({'name': lists2['name'][x2], 'value': lists2['value'][x2]})
        elif request.GET['type'] == 'yuezhongduanfenxi':
            qyear = request.GET['year']
            qmonth = request.GET['month']
            qshuming = request.GET['shuming']
            qleiming = request.GET['leiming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list("shijian",
                                                                                                              "zhongduanfenxi")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and (qleiming in ob[1]):
                        for x in range(len(ob[1]['zhongduanming'])):
                            lists1.append({'name': ob[1]['zhongduanming'][x], 'value': ob[1][qleiming][x],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            # print(df1)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['name'], as_index=False).agg({'value': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            for x2 in range(len(lists2['name'])):
                lists.append({'name': lists2['name'][x2], 'value': lists2['value'][x2]})
        elif request.GET['type'] == 'nianzhongduanfenxi':
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            qleiming = request.GET['leiming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear).values_list("shijian", "zhongduanfenxi")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and (qleiming in ob[1]):
                        for x in range(len(ob[1]['zhongduanming'])):
                            lists1.append({'name': ob[1]['zhongduanming'][x], 'value': ob[1][qleiming][x],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            # print(df1)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['name'], as_index=False).agg({'value': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            for x2 in range(len(lists2['name'])):
                lists.append({'name': lists2['name'][x2], 'value': lists2['value'][x2]})
        elif request.GET['type'] == 'atyonghuhuaxiang':
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Yonghufenxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                         "yonghuhuaxiang")
            lists1 = []
            lists2 = []
            lists3 = []
            lists4 = []
            lists5 = []
            for ob in ulist:
                if (ob[1] != None) and ("xingbie" in ob[1]):
                    for x in range(len(ob[1]['xingbie'])):
                        lists1.append({'name': ob[1]['xingbie'][x]['name'], 'value': ob[1]['xingbie'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['nianling'])):
                        lists2.append({'name': ob[1]['nianling'][x]['name'], 'value': ob[1]['nianling'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['jiaoyu'])):
                        lists3.append({'name': ob[1]['jiaoyu'][x]['name'], 'value': ob[1]['jiaoyu'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['hangye'])):
                        lists4.append({'name': ob[1]['hangye'][x]['name'], 'value': ob[1]['hangye'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['xingqu'])):
                        lists5.append({'name': ob[1]['xingqu'][x]['name'], 'value': ob[1]['xingqu'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            # df1 = pd.DataFrame(ulist)
            df1 = pd.DataFrame(lists1)
            df2 = pd.DataFrame(lists2)
            df3 = pd.DataFrame(lists3)
            df4 = pd.DataFrame(lists4)
            df5 = pd.DataFrame(lists5)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee = group_name.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss1 = group_namee.to_dict(orient='list')
            group_name2 = df2.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee2 = group_name2.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss2 = group_namee2.to_dict(orient='list')
            group_name3 = df3.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee3 = group_name3.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss3 = group_namee3.to_dict(orient='list')
            group_name4 = df4.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee4 = group_name4.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss4 = group_namee4.to_dict(orient='list')
            group_name5 = df5.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee5 = group_name5.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss5 = group_namee5.to_dict(orient='list')
            lists = {}
            lists['xingbie'] = []
            lists['nianling'] = []
            lists['jiaoyu'] = []
            lists['hangye'] = []
            lists['xingqu'] = []
            for x1 in range(len(listss1['name'])):
                lists['xingbie'].append({'name': listss1['name'][x1], 'value': listss1['value'][x1]})
            for x2 in range(len(listss2['name'])):
                lists['nianling'].append({'name': listss2['name'][x2], 'value': listss2['value'][x2]})
            for x3 in range(len(listss3['name'])):
                lists['jiaoyu'].append({'name': listss3['name'][x3], 'value': listss3['value'][x3]})
            for x4 in range(len(listss4['name'])):
                lists['hangye'].append({'name': listss4['name'][x4], 'value': listss4['value'][x4]})
            for x5 in range(len(listss5['name'])):
                lists['xingqu'].append({'name': listss5['name'][x5], 'value': listss5['value'][x5]})
        elif request.GET['type'] == 'yueyonghuhuaxiang':
            qyear = request.GET['year']
            qmonth = request.GET['month']
            qshuming = request.GET['shuming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list("shijian",
                                                                                                              "yonghuhuaxiang")
            lists1 = []
            lists2 = []
            lists3 = []
            lists4 = []
            lists5 = []
            for ob in ulist:
                if (ob[1] != None) and ("xingbie" in ob[1]):
                    for x in range(len(ob[1]['xingbie'])):
                        lists1.append({'name': ob[1]['xingbie'][x]['name'], 'value': ob[1]['xingbie'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['nianling'])):
                        lists2.append({'name': ob[1]['nianling'][x]['name'], 'value': ob[1]['nianling'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['jiaoyu'])):
                        lists3.append({'name': ob[1]['jiaoyu'][x]['name'], 'value': ob[1]['jiaoyu'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['hangye'])):
                        lists4.append({'name': ob[1]['hangye'][x]['name'], 'value': ob[1]['hangye'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['xingqu'])):
                        lists5.append({'name': ob[1]['xingqu'][x]['name'], 'value': ob[1]['xingqu'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            # df1 = pd.DataFrame(ulist)
            df1 = pd.DataFrame(lists1)
            df2 = pd.DataFrame(lists2)
            df3 = pd.DataFrame(lists3)
            df4 = pd.DataFrame(lists4)
            df5 = pd.DataFrame(lists5)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee = group_name.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss1 = group_namee.to_dict(orient='list')
            group_name2 = df2.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee2 = group_name2.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss2 = group_namee2.to_dict(orient='list')
            group_name3 = df3.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee3 = group_name3.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss3 = group_namee3.to_dict(orient='list')
            group_name4 = df4.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee4 = group_name4.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss4 = group_namee4.to_dict(orient='list')
            group_name5 = df5.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee5 = group_name5.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss5 = group_namee5.to_dict(orient='list')
            lists = {}
            lists['xingbie'] = []
            lists['nianling'] = []
            lists['jiaoyu'] = []
            lists['hangye'] = []
            lists['xingqu'] = []
            for x1 in range(len(listss1['name'])):
                lists['xingbie'].append({'name': listss1['name'][x1], 'value': listss1['value'][x1]})
            for x2 in range(len(listss2['name'])):
                lists['nianling'].append({'name': listss2['name'][x2], 'value': listss2['value'][x2]})
            for x3 in range(len(listss3['name'])):
                lists['jiaoyu'].append({'name': listss3['name'][x3], 'value': listss3['value'][x3]})
            for x4 in range(len(listss4['name'])):
                lists['hangye'].append({'name': listss4['name'][x4], 'value': listss4['value'][x4]})
            for x5 in range(len(listss5['name'])):
                lists['xingqu'].append({'name': listss5['name'][x5], 'value': listss5['value'][x5]})
        elif request.GET['type'] == 'nianyonghuhuaxiang':
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear).values_list("shijian", "yonghuhuaxiang")
            lists1 = []
            lists2 = []
            lists3 = []
            lists4 = []
            lists5 = []
            for ob in ulist:
                if (ob[1] != None) and ("xingbie" in ob[1]):
                    for x in range(len(ob[1]['xingbie'])):
                        lists1.append({'name': ob[1]['xingbie'][x]['name'], 'value': ob[1]['xingbie'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['nianling'])):
                        lists2.append({'name': ob[1]['nianling'][x]['name'], 'value': ob[1]['nianling'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['jiaoyu'])):
                        lists3.append({'name': ob[1]['jiaoyu'][x]['name'], 'value': ob[1]['jiaoyu'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['hangye'])):
                        lists4.append({'name': ob[1]['hangye'][x]['name'], 'value': ob[1]['hangye'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['xingqu'])):
                        lists5.append({'name': ob[1]['xingqu'][x]['name'], 'value': ob[1]['xingqu'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            # df1 = pd.DataFrame(ulist)
            df1 = pd.DataFrame(lists1)
            df2 = pd.DataFrame(lists2)
            df3 = pd.DataFrame(lists3)
            df4 = pd.DataFrame(lists4)
            df5 = pd.DataFrame(lists5)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee = group_name.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss1 = group_namee.to_dict(orient='list')
            group_name2 = df2.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee2 = group_name2.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss2 = group_namee2.to_dict(orient='list')
            group_name3 = df3.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee3 = group_name3.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss3 = group_namee3.to_dict(orient='list')
            group_name4 = df4.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee4 = group_name4.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss4 = group_namee4.to_dict(orient='list')
            group_name5 = df5.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee5 = group_name5.groupby(['name'], as_index=False).agg({'value': qshuming})
            listss5 = group_namee5.to_dict(orient='list')
            lists = {}
            lists['xingbie'] = []
            lists['nianling'] = []
            lists['jiaoyu'] = []
            lists['hangye'] = []
            lists['xingqu'] = []
            for x1 in range(len(listss1['name'])):
                lists['xingbie'].append({'name': listss1['name'][x1], 'value': listss1['value'][x1]})
            for x2 in range(len(listss2['name'])):
                lists['nianling'].append({'name': listss2['name'][x2], 'value': listss2['value'][x2]})
            for x3 in range(len(listss3['name'])):
                lists['jiaoyu'].append({'name': listss3['name'][x3], 'value': listss3['value'][x3]})
            for x4 in range(len(listss4['name'])):
                lists['hangye'].append({'name': listss4['name'][x4], 'value': listss4['value'][x4]})
            for x5 in range(len(listss5['name'])):
                lists['xingqu'].append({'name': listss5['name'][x5], 'value': listss5['value'][x5]})
        elif request.GET['type'] == 'atshiyongxiguan':
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Shiyongxingwei.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                         "shiyongxiguan")
            lists1 = []
            lists2 = []
            lists3 = []
            for ob in ulist:
                if (ob[1] != None) and ("time" in ob[1]):
                    for x in range(len(ob[1]['time'])):
                        lists1.append({'name': ob[1]['time'][x]['name'], 'value': ob[1]['time'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['depth'])):
                        lists2.append({'name': ob[1]['depth'][x]['name'], 'value': ob[1]['depth'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['frequency'])):
                        lists3.append({'name': ob[1]['frequency'][x]['name'], 'value': ob[1]['frequency'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            # df1 = pd.DataFrame(ulist)
            df1 = pd.DataFrame(lists1)
            df2 = pd.DataFrame(lists2)
            df3 = pd.DataFrame(lists3)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee = group_name.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss1 = group_namee.to_dict(orient='list')
            group_name2 = df2.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee2 = group_name2.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss2 = group_namee2.to_dict(orient='list')
            group_name3 = df3.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee3 = group_name3.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss3 = group_namee3.to_dict(orient='list')
            lists = {}
            lists['time'] = []
            lists['depth'] = []
            lists['frequency'] = []
            for x1 in range(len(listss1['name'])):
                lists['time'].append({'name': listss1['name'][x1], 'value': listss1['value'][x1]})
            for x2 in range(len(listss2['name'])):
                lists['depth'].append({'name': listss2['name'][x2], 'value': listss2['value'][x2]})
            for x3 in range(len(listss3['name'])):
                lists['frequency'].append({'name': listss3['name'][x3], 'value': listss3['value'][x3]})
        elif request.GET['type'] == 'yueshiyongxiguan':
            qyear = request.GET['year']
            qmonth = request.GET['month']
            qshuming = request.GET['shuming']
            ulist = models.Shiyongxingwei.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list("shijian",
                                                                                                              "shiyongxiguan")
            lists1 = []
            lists2 = []
            lists3 = []
            for ob in ulist:
                if (ob[1] != None) and ("time" in ob[1]):
                    for x in range(len(ob[1]['time'])):
                        lists1.append({'name': ob[1]['time'][x]['name'], 'value': ob[1]['time'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['depth'])):
                        lists2.append({'name': ob[1]['depth'][x]['name'], 'value': ob[1]['depth'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['frequency'])):
                        lists3.append({'name': ob[1]['frequency'][x]['name'], 'value': ob[1]['frequency'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            # df1 = pd.DataFrame(ulist)
            df1 = pd.DataFrame(lists1)
            df2 = pd.DataFrame(lists2)
            df3 = pd.DataFrame(lists3)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee = group_name.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss1 = group_namee.to_dict(orient='list')
            group_name2 = df2.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee2 = group_name2.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss2 = group_namee2.to_dict(orient='list')
            group_name3 = df3.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee3 = group_name3.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss3 = group_namee3.to_dict(orient='list')
            lists = {}
            lists['time'] = []
            lists['depth'] = []
            lists['frequency'] = []
            for x1 in range(len(listss1['name'])):
                lists['time'].append({'name': listss1['name'][x1], 'value': listss1['value'][x1]})
            for x2 in range(len(listss2['name'])):
                lists['depth'].append({'name': listss2['name'][x2], 'value': listss2['value'][x2]})
            for x3 in range(len(listss3['name'])):
                lists['frequency'].append({'name': listss3['name'][x3], 'value': listss3['value'][x3]})
        elif request.GET['type'] == 'nianshiyongxiguan':
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Shiyongxingwei.objects.filter(shijian__year=qyear).values_list("shijian", "shiyongxiguan")
            lists1 = []
            lists2 = []
            lists3 = []
            for ob in ulist:
                if (ob[1] != None) and ("time" in ob[1]):
                    for x in range(len(ob[1]['time'])):
                        lists1.append({'name': ob[1]['time'][x]['name'], 'value': ob[1]['time'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['depth'])):
                        lists2.append({'name': ob[1]['depth'][x]['name'], 'value': ob[1]['depth'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
                    for x in range(len(ob[1]['frequency'])):
                        lists3.append({'name': ob[1]['frequency'][x]['name'], 'value': ob[1]['frequency'][x]['value'],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            # df1 = pd.DataFrame(ulist)
            df1 = pd.DataFrame(lists1)
            df2 = pd.DataFrame(lists2)
            df3 = pd.DataFrame(lists3)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee = group_name.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss1 = group_namee.to_dict(orient='list')
            group_name2 = df2.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee2 = group_name2.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss2 = group_namee2.to_dict(orient='list')
            group_name3 = df3.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_namee3 = group_name3.groupby(['name'], as_index=False).agg({'value': 'mean'})
            listss3 = group_namee3.to_dict(orient='list')
            lists = {}
            lists['time'] = []
            lists['depth'] = []
            lists['frequency'] = []
            for x1 in range(len(listss1['name'])):
                lists['time'].append({'name': listss1['name'][x1], 'value': listss1['value'][x1]})
            for x2 in range(len(listss2['name'])):
                lists['depth'].append({'name': listss2['name'][x2], 'value': listss2['value'][x2]})
            for x3 in range(len(listss3['name'])):
                lists['frequency'].append({'name': listss3['name'][x3], 'value': listss3['value'][x3]})
        elif (request.GET['type'] == 'atyemianlujing'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Shiyongxingwei.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                            "yemianlujing")
            lists1 = {}
            lists1['yemianlie'] = []
            lists1['yemianguanxi'] = []
            lists = {}
            lists['yemianlie'] = []
            lists['yemianguanxi'] = []
            for ob in ulist:
                if (ob[1] != None) and ("yemianguanxi" in ob[1]):
                    for x in range(len(ob[1]['yemianlie'])):
                        lists1['yemianlie'].append(ob[1]['yemianlie'][x])
                    for x in range(len(ob[1]['yemianguanxi'])):
                        lists1['yemianguanxi'].append(
                            {"shijian": ob[0].strftime('%Y-%m-%d'), "source": ob[1]['yemianguanxi'][x]['source'],
                             "target": ob[1]['yemianguanxi'][x]['target'],
                             "value": int(ob[1]['yemianguanxi'][x]['value'])})
            df1 = pd.DataFrame(lists1['yemianguanxi'])
            group_name = df1.groupby(['shijian', 'source', 'target'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['source', 'target'], as_index=False).agg({'value': 'mean'})
            lists3 = group_name1.to_dict(orient='list')
            for x2 in range(len(lists3['source'])):
                lists['yemianguanxi'].append(
                    {'source': lists3['source'][x2], 'target': lists3['target'][x2], 'value': lists3['value'][x2]})
            lists['yemianlie'] = utils.list_dict_duplicate_removal(lists1['yemianlie'])
        elif (request.GET['type'] == 'yueyemianlujing'):
            qyear = request.GET['year']
            qmonth = request.GET['month']
            ulist = models.Shiyongxingwei.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list(
                "shijian", "yemianlujing")
            lists1 = {}
            lists1['yemianlie'] = []
            lists1['yemianguanxi'] = []
            lists = {}
            lists['yemianlie'] = []
            lists['yemianguanxi'] = []
            for ob in ulist:
                if (ob[1] != None) and ("yemianguanxi" in ob[1]):
                    for x in range(len(ob[1]['yemianlie'])):
                        lists1['yemianlie'].append(ob[1]['yemianlie'][x])
                    for x in range(len(ob[1]['yemianguanxi'])):
                        lists1['yemianguanxi'].append(
                            {"shijian": ob[0].strftime('%Y-%m-%d'), "source": ob[1]['yemianguanxi'][x]['source'],
                             "target": ob[1]['yemianguanxi'][x]['target'],
                             "value": int(ob[1]['yemianguanxi'][x]['value'])})
            df1 = pd.DataFrame(lists1['yemianguanxi'])
            group_name = df1.groupby(['shijian', 'source', 'target'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['source', 'target'], as_index=False).agg({'value': 'mean'})
            lists3 = group_name1.to_dict(orient='list')
            for x2 in range(len(lists3['source'])):
                lists['yemianguanxi'].append(
                    {'source': lists3['source'][x2], 'target': lists3['target'][x2], 'value': lists3['value'][x2]})
            lists['yemianlie'] = utils.list_dict_duplicate_removal(lists1['yemianlie'])
        elif (request.GET['type'] == 'nianyemianlujing'):
            qyear = request.GET['year']
            ulist = models.Shiyongxingwei.objects.filter(shijian__year=qyear).values_list("shijian", "yemianlujing")
            lists1 = {}
            lists1['yemianlie'] = []
            lists1['yemianguanxi'] = []
            lists = {}
            lists['yemianlie'] = []
            lists['yemianguanxi'] = []
            for ob in ulist:
                if (ob[1] != None) and ("yemianguanxi" in ob[1]):
                    for x in range(len(ob[1]['yemianlie'])):
                        lists1['yemianlie'].append(ob[1]['yemianlie'][x])
                    for x in range(len(ob[1]['yemianguanxi'])):
                        lists1['yemianguanxi'].append(
                            {"shijian": ob[0].strftime('%Y-%m-%d'), "source": ob[1]['yemianguanxi'][x]['source'],
                             "target": ob[1]['yemianguanxi'][x]['target'],
                             "value": int(ob[1]['yemianguanxi'][x]['value'])})
            df1 = pd.DataFrame(lists1['yemianguanxi'])
            group_name = df1.groupby(['shijian', 'source', 'target'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['source', 'target'], as_index=False).agg({'value': 'mean'})
            lists3 = group_name1.to_dict(orient='list')
            for x2 in range(len(lists3['source'])):
                lists['yemianguanxi'].append(
                    {'source': lists3['source'][x2], 'target': lists3['target'][x2], 'value': lists3['value'][x2]})
            lists['yemianlie'] = utils.list_dict_duplicate_removal(lists1['yemianlie'])
        elif (request.GET['type'] == 'xinyonghuliucunfenxi'):
            start_date0 = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date0 = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            start_date = utils.get_qian_time(datetime.datetime.strptime(start_date0, "%Y-%m-%d %H:%M:%S"), "30天前")
            end_date = utils.get_hou_time(datetime.datetime.strptime(end_date0, "%Y-%m-%d %H:%M:%S"), "30天后")
            ulist = models.Liucunfenxi.objects.filter(shijian__range=(start_date, end_date)).values_list(
                "xinyonghushouciliucun")
            start_date1 = parse(start_date0).strftime('%Y-%m-%d')
            end_date1 = parse(end_date0).strftime('%Y-%m-%d')
            lists1 = {}
            lists1['riqi'] = []
            lists1['new'] = []
            lists1['day2'] = []
            lists1['day3'] = []
            lists1['day4'] = []
            lists1['day5'] = []
            lists1['day6'] = []
            lists1['day7'] = []
            lists1['day15'] = []
            lists1['day30'] = []
            for ob in ulist:
                if (ob[0] != None) and ("riqi" in ob[0]):
                    for x in range(len(ob[0]['riqi'])):
                        lists1['riqi'].append((ob[0]['riqi'][x][0]).replace('/', '-'))
                        # print(ob[0]['xinyonghushu'][x])
                        lists1['new'].append(0 if len(ob[0]['xinyonghushu'][x]) <= 0 else ob[0]['xinyonghushu'][x][0])
                        lists1['day2'].append(0 if len(ob[0]['xinyonghushu'][x]) <= 1 else ob[0]['xinyonghushu'][x][1])
                        lists1['day3'].append(0 if len(ob[0]['xinyonghushu'][x]) <= 2 else ob[0]['xinyonghushu'][x][2])
                        lists1['day4'].append(0 if len(ob[0]['xinyonghushu'][x]) <= 3 else ob[0]['xinyonghushu'][x][3])
                        lists1['day5'].append(0 if len(ob[0]['xinyonghushu'][x]) <= 4 else ob[0]['xinyonghushu'][x][4])
                        lists1['day6'].append(0 if len(ob[0]['xinyonghushu'][x]) <= 5 else ob[0]['xinyonghushu'][x][5])
                        lists1['day7'].append(0 if len(ob[0]['xinyonghushu'][x]) <= 6 else ob[0]['xinyonghushu'][x][6])
                        lists1['day15'].append(0 if len(ob[0]['xinyonghushu'][x]) <= 7 else ob[0]['xinyonghushu'][x][7])
                        lists1['day30'].append(0 if len(ob[0]['xinyonghushu'][x]) <= 8 else ob[0]['xinyonghushu'][x][8])
            df1 = pd.DataFrame(lists1)
            df1['riqi'] = pd.to_datetime(df1['riqi'])  # 批量类型转换
            df2 = df1.set_index('riqi', inplace=False, drop=False)
            df1 = (df2.loc[start_date1:end_date1, 'riqi': 'day30']).reset_index(drop=True)
            df1['riqi'] = df1['riqi'].apply(lambda x: x.strftime('%Y-%m-%d'))  # 批量类型转换
            group_name = df1.groupby(['riqi'], as_index=False).agg(
                {'new': 'max', 'day2': 'max', 'day3': 'max', 'day4': 'max', 'day5': 'max', 'day6': 'max', 'day7': 'max',
                 'day15': 'max', 'day30': 'max'})
            lists3 = group_name.to_dict(orient='list')
            for x2 in range(len(lists3['riqi'])):
                lists.append(
                    {'riqi': lists3['riqi'][x2], 'new': lists3['new'][x2], 'day2': lists3['day2'][x2],
                     'day3': lists3['day3'][x2], 'day4': lists3['day4'][x2], 'day5': lists3['day5'][x2],
                     'day6': lists3['day6'][x2], 'day7': lists3['day7'][x2], 'day15': lists3['day15'][x2],
                     'day30': lists3['day30'][x2]})
        elif (request.GET['type'] == 'huoyueyonghuliucunfenxi'):
            start_date0 = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date0 = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            start_date = utils.get_qian_time(datetime.datetime.strptime(start_date0, "%Y-%m-%d %H:%M:%S"), "30天前")
            end_date = utils.get_hou_time(datetime.datetime.strptime(end_date0, "%Y-%m-%d %H:%M:%S"), "30天后")
            ulist = models.Liucunfenxi.objects.filter(shijian__range=(start_date, end_date)).values_list(
                "huoyueyonghuliucun")
            start_date1 = parse(start_date0).strftime('%Y-%m-%d')
            end_date1 = parse(end_date0).strftime('%Y-%m-%d')
            lists1 = {}
            lists1['riqi'] = []
            lists1['active'] = []
            lists1['day2'] = []
            lists1['day3'] = []
            lists1['day4'] = []
            lists1['day5'] = []
            lists1['day6'] = []
            lists1['day7'] = []
            lists1['day15'] = []
            lists1['day30'] = []
            for ob in ulist:
                if (ob[0] != None) and ("riqi" in ob[0]):
                    for x in range(len(ob[0]['riqi'])):
                        lists1['riqi'].append((ob[0]['riqi'][x][0]).replace('/', '-'))
                        # print(ob[0]['xinyonghushu'][x])
                        lists1['active'].append(
                            0 if len(ob[0]['huoyueyonghushu'][x]) <= 0 else ob[0]['huoyueyonghushu'][x][0])
                        lists1['day2'].append(
                            0 if len(ob[0]['huoyueyonghushu'][x]) <= 1 else ob[0]['huoyueyonghushu'][x][1])
                        lists1['day3'].append(
                            0 if len(ob[0]['huoyueyonghushu'][x]) <= 2 else ob[0]['huoyueyonghushu'][x][2])
                        lists1['day4'].append(
                            0 if len(ob[0]['huoyueyonghushu'][x]) <= 3 else ob[0]['huoyueyonghushu'][x][3])
                        lists1['day5'].append(
                            0 if len(ob[0]['huoyueyonghushu'][x]) <= 4 else ob[0]['huoyueyonghushu'][x][4])
                        lists1['day6'].append(
                            0 if len(ob[0]['huoyueyonghushu'][x]) <= 5 else ob[0]['huoyueyonghushu'][x][5])
                        lists1['day7'].append(
                            0 if len(ob[0]['huoyueyonghushu'][x]) <= 6 else ob[0]['huoyueyonghushu'][x][6])
                        lists1['day15'].append(
                            0 if len(ob[0]['huoyueyonghushu'][x]) <= 7 else ob[0]['huoyueyonghushu'][x][7])
                        lists1['day30'].append(
                            0 if len(ob[0]['huoyueyonghushu'][x]) <= 8 else ob[0]['huoyueyonghushu'][x][8])
            df1 = pd.DataFrame(lists1)
            df1['riqi'] = pd.to_datetime(df1['riqi'])  # 批量类型转换
            df2 = df1.set_index('riqi', inplace=False, drop=False)
            df1 = (df2.loc[start_date1:end_date1, 'riqi': 'day30']).reset_index(drop=True)
            df1['riqi'] = df1['riqi'].apply(lambda x: x.strftime('%Y-%m-%d'))  # 批量类型转换
            group_name = df1.groupby(['riqi'], as_index=False).agg(
                {'active': 'max', 'day2': 'max', 'day3': 'max', 'day4': 'max', 'day5': 'max', 'day6': 'max',
                 'day7': 'max', 'day15': 'max', 'day30': 'max'})
            lists3 = group_name.to_dict(orient='list')
            for x2 in range(len(lists3['riqi'])):
                lists.append(
                    {'riqi': lists3['riqi'][x2], 'active': lists3['active'][x2], 'day2': lists3['day2'][x2],
                     'day3': lists3['day3'][x2], 'day4': lists3['day4'][x2], 'day5': lists3['day5'][x2],
                     'day6': lists3['day6'][x2], 'day7': lists3['day7'][x2], 'day15': lists3['day15'][x2],
                     'day30': lists3['day30'][x2]})
        elif (request.GET['type'] == 'xinyonghuzidingyiliucun'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Liucunfenxi.objects.filter(shijian__range=(start_date, end_date)).values_list(
                "xinyonghuzidingyiliucun")
            lists1 = []
            for ob in ulist:
                if (ob[0] != None) and ("riqi" in ob[0]):
                    for x in range(len(ob[0]['riqi'])):
                        lists1.append(
                            {'riqi': (ob[0]['riqi'][x][0]).replace('/', '-'), 'xinyonghushu': ob[0]['xinyonghushu'][x],
                             'liucunyonghushu': ob[0]['liucunyonghushu'][x]})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['riqi'], as_index=False).max(['xinyonghushu', 'liucunyonghushu'])
            lists = group_name.to_dict(orient='list')
        elif (request.GET['type'] == 'nianyonghuqushi'):
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear).values_list("shijian", "yonghuqushi")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and ("laoyonghu" in ob[1]):
                        for x in range(len(ob[1]['laoyonghu'])):
                            lists1.append({'laoyonghu': ob[1]['laoyonghu'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                           'xinyonghu': ob[1]['xinyonghu'][x], 'qidongyonghu': ob[1]['qidongyonghu'][x],
                                           'banbenlaoyonghu': ob[1]['banbenlaoyonghu'][x],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian'], as_index=False).max(
                ['laoyonghu', 'xinyonghu', 'qidongcishu', 'qidongyonghu', 'banbenlaoyonghu'])
            shijian_id = group_name.pop('shijian')  # // shijian获取索列
            # // shijian重新取值，取索列第一位字符
            shijian_id = shijian_id.str[:7]
            # // 在第二列新增shijian列并用shijian（第一位字符）填充
            group_name.insert(0, 'shijian', shijian_id)
            group_name1 = group_name.groupby(['shijian'], as_index=False).agg(
                {'laoyonghu': qshuming, 'xinyonghu': qshuming, 'qidongcishu': qshuming, 'qidongyonghu': qshuming,
                 'banbenlaoyonghu': qshuming})
            lists = group_name1.to_dict(orient='list')
        elif (request.GET['type'] == 'nianxinyonghuzidingyiliucun'):
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Liucunfenxi.objects.filter(shijian__year=qyear).values_list("xinyonghuzidingyiliucun")
            lists1 = []
            for ob in ulist:
                if (ob[0] != None) and ("riqi" in ob[0]):
                    for x in range(len(ob[0]['riqi'])):
                        lists1.append(
                            {'riqi': (ob[0]['riqi'][x][0]).replace('/', '-'), 'xinyonghushu': ob[0]['xinyonghushu'][x],
                             'liucunyonghushu': ob[0]['liucunyonghushu'][x]})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['riqi'], as_index=False).max(['xinyonghushu', 'liucunyonghushu'])
            riqi_id = group_name.pop('riqi')  # // riqi获取索列
            # // riqi重新取值，取索列第一位字符
            riqi_id = riqi_id.str[:7]
            # // 在第二列新增riqi列并用riqi（第一位字符）填充
            group_name.insert(0, 'riqi', riqi_id)
            group_name1 = group_name.groupby(['riqi'], as_index=False).agg({'xinyonghushu': qshuming, 'liucunyonghushu': qshuming})
            lists = group_name1.to_dict(orient='list')
        elif (request.GET['type'] == 'shijianming'):
            ulist = models.Shijianxinxi.objects.last()
            lists1 = ulist.shijianfenxi
            lists2 = utils.list_dict_duplicate_removal(lists1['shijianming'])
            for xew in range(len(lists2)):
                lists.append({"value": lists2[xew], "label": lists2[xew]})
        elif (request.GET['type'] == 'yueatshijianfenxi'):
            qyear = request.GET['year']
            qmonth = request.GET['month']
            qleiming = request.GET['leiming']
            qshileiming = request.GET['shileiming']
            ulist = models.Shijianxinxi.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list(
                "shijian", "shijianfenxi")
            lists1 = []
            lists = {}
            lists['shuju'] = []
            lists['riqi'] = []
            for ob in ulist:
                if (ob[1] != None) and (qleiming in ob[1]):
                    for x in range(len(ob[1]['shijianming'])):
                        lists1.append({'name': ob[1]['shijianming'][x], 'value': ob[1][qleiming][x],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            arryshileiming = qshileiming.split(',')
            if len(arryshileiming) >= 1:
                df2 = df1.set_index('name', inplace=False, drop=False)
                df1 = (df2.loc[arryshileiming, ['name', 'shijian', 'value']]).reset_index(drop=True)
            group_name = df1.groupby('shijian')
            for name, group in group_name:
                group1 = group.groupby(['name'], as_index=False).max('value')
                lists2 = group1.to_dict(orient='list')
                listst1 = {}
                listst1['series'] = []
                listst1['title'] = {'text': name + " 事件分析"}
                riqisum = 0
                for tub in range(len(lists2['value'])):
                    riqisum += lists2['value'][tub]
                    listst1['series'].append({'name': lists2['name'][tub], 'value': lists2['value'][tub]})
                lists['riqi'].append([name, riqisum])
                lists['shuju'].append(listst1)
        elif (request.GET['type'] == 'riqiduanatshijianfenxi'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            qshuming = request.GET['shuming']
            ulist = models.Shijianxinxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                          "shijianfenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("shijianming" in ob[1]):
                    for x in range(len(ob[1]['shijianming'])):
                        lists1.append(
                            {'chufacishu': ob[1]['chufacishu'][x], 'chufayonghushu': ob[1]['chufayonghushu'][x],
                             'shijianpingjun': ob[1]['shijianpingjun'][x], 'shijianming': ob[1]['shijianming'][x],
                             'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'shijianming'], as_index=False).agg(
                {'chufacishu': 'max', 'chufayonghushu': 'max', 'shijianpingjun': 'max'})
            group1 = group_name.groupby(['shijianming'], as_index=False).agg(
                {'chufacishu': qshuming, 'chufayonghushu': qshuming, 'shijianpingjun': qshuming})
            lists = group1.to_dict(orient='list')
            lists['chufacishu'] = np.around(np.array(lists['chufacishu']), decimals=0).tolist()
            lists['chufayonghushu'] = np.around(np.array(lists['chufayonghushu']), decimals=0).tolist()
            lists['shijianpingjun'] = np.around(np.array(lists['shijianpingjun']), decimals=0).tolist()
            # print(lists)
        elif (request.GET['type'] == 'dongtaiaxshshijianfenxi'):
            start_date = parse(request.GET['riqi']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['riqi']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Shijianxinxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                          "shijianfenxi")
            lists1 = []
            lists2 = []
            lists = {}
            lists['shuju'] = []
            lists['riqi'] = []
            lists['shijianming'] = []
            for ob in ulist:
                if (ob[1] != None) and ("shijianming" in ob[1]):
                    for x in range(len(ob[1]['shijianming'])):
                        lists1.append(
                            {'chufacishu': ob[1]['chufacishu'][x], 'chufayonghushu': ob[1]['chufayonghushu'][x],
                             'shijianpingjun': ob[1]['shijianpingjun'][x], 'shijianming': ob[1]['shijianming'][x],
                             'shijian': ob[0].strftime('%H')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby('shijian')
            for name, group in group_name:
                group1 = group.groupby(['shijianming'], as_index=False).max(
                    ['chufacishu', 'chufayonghushu', 'shijianpingjun'])
                lists2 = group1.to_dict(orient='list')
                listst1 = {}
                listst1['series'] = []
                listst11 = []
                listst12 = []
                listst13 = []
                lists['riqi'].append(name)
                listst1['title'] = {'text': name + "点-事件分析"}
                for tub in range(len(lists2['chufacishu'])):
                    listst11.append({'name': lists2['shijianming'][tub], 'value': lists2['chufacishu'][tub]})
                    listst12.append({'name': lists2['shijianming'][tub], 'value': lists2['chufayonghushu'][tub]})
                    listst13.append({'name': lists2['shijianming'][tub], 'value': lists2['shijianpingjun'][tub]})
                listst1['series'].append({'data': listst11})
                listst1['series'].append({'data': listst12})
                listst1['series'].append({'data': listst13})
                lists['shuju'].append(listst1)
            # for bure in range(len(lists2['shijianming'])):
            #     lists['shijianming'].append((re.sub(r"(.{2})", "\\1\n", lists2['shijianming'][bure])))
            lists['shijianming'] = lists2['shijianming']
        elif (request.GET['type'] == 'dongtaiatshijianfenxi'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Shijianxinxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                          "shijianfenxi")
            lists1 = []
            lists2 = []
            lists = {}
            lists['shuju'] = []
            lists['riqi'] = []
            lists['shijianming'] = []
            for ob in ulist:
                if (ob[1] != None) and ("shijianming" in ob[1]):
                    for x in range(len(ob[1]['shijianming'])):
                        lists1.append(
                            {'chufacishu': ob[1]['chufacishu'][x], 'chufayonghushu': ob[1]['chufayonghushu'][x],
                             'shijianpingjun': ob[1]['shijianpingjun'][x], 'shijianming': ob[1]['shijianming'][x],
                             'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby('shijian')
            for name, group in group_name:
                group1 = group.groupby(['shijianming'], as_index=False).max(
                    ['chufacishu', 'chufayonghushu', 'shijianpingjun'])
                lists2 = group1.to_dict(orient='list')
                listst1 = {}
                listst1['series'] = []
                listst11 = []
                listst12 = []
                listst13 = []
                lists['riqi'].append(name)
                listst1['title'] = {'text': name + "日-事件分析"}
                for tub in range(len(lists2['chufacishu'])):
                    listst11.append({'name': lists2['shijianming'][tub], 'value': lists2['chufacishu'][tub]})
                    listst12.append({'name': lists2['shijianming'][tub], 'value': lists2['chufayonghushu'][tub]})
                    listst13.append({'name': lists2['shijianming'][tub], 'value': lists2['shijianpingjun'][tub]})
                listst1['series'].append({'data': listst11})
                listst1['series'].append({'data': listst12})
                listst1['series'].append({'data': listst13})
                lists['shuju'].append(listst1)
            # for bure in range(len(lists2['shijianming'])):
            #     lists['shijianming'].append((re.sub(r"(.{2})", "\\1\n", lists2['shijianming'][bure])))
            lists['shijianming'] = lists2['shijianming']
        elif (request.GET['type'] == 'dongtaiayshijianfenxi'):
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Shijianxinxi.objects.filter(shijian__year=qyear).values_list("shijian", "shijianfenxi")
            lists1 = []
            lists2 = []
            lists = {}
            lists['shuju'] = []
            lists['riqi'] = []
            lists['shijianming'] = []
            for ob in ulist:
                if (ob[1] != None) and ("shijianming" in ob[1]):
                    for x in range(len(ob[1]['shijianming'])):
                        lists1.append(
                            {'chufacishu': ob[1]['chufacishu'][x], 'chufayonghushu': ob[1]['chufayonghushu'][x],
                             'shijianpingjun': ob[1]['shijianpingjun'][x], 'shijianming': ob[1]['shijianming'][x],
                             'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'shijianming'], as_index=False).agg(
                {'chufacishu': 'max', 'chufayonghushu': 'max', 'shijianpingjun': 'max'})
            shijian_id = group_name.pop('shijian')  # // book_id获取索书号列
            # // book_id重新取值，取索书号列第一位字符
            shijian_id = shijian_id.str[:7]
            # // 在第二列新增book_id列并用book_id（第一位字符）填充
            group_name.insert(0, 'shijian', shijian_id)
            group_name1 = group_name.groupby('shijian')
            for name, group in group_name1:
                group1 = group.groupby(['shijianming'], as_index=False).agg(
                    {'chufacishu': qshuming, 'chufayonghushu': qshuming, 'shijianpingjun': qshuming})
                lists2 = group1.to_dict(orient='list')
                listst1 = {}
                listst1['series'] = []
                listst11 = []
                listst12 = []
                listst13 = []
                lists['riqi'].append(name)
                listst1['title'] = {'text': name + "月-事件分析"}
                for tub in range(len(lists2['chufacishu'])):
                    listst11.append({'name': lists2['shijianming'][tub], 'value': lists2['chufacishu'][tub]})
                    listst12.append({'name': lists2['shijianming'][tub], 'value': lists2['chufayonghushu'][tub]})
                    listst13.append({'name': lists2['shijianming'][tub], 'value': lists2['shijianpingjun'][tub]})
                listst1['series'].append({'data': listst11})
                listst1['series'].append({'data': listst12})
                listst1['series'].append({'data': listst13})
                lists['shuju'].append(listst1)
            # for bure in range(len(lists2['shijianming'])):
            #     lists['shijianming'].append((re.sub(r"(.{2})", "\\1\n", lists2['shijianming'][bure])))
            lists['shijianming'] = lists2['shijianming']
        elif (request.GET['type'] == 'bzhchriqishijianfenxi'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Shijianxinxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                          "shijianfenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("shijianming" in ob[1]):
                    for x in range(len(ob[1]['shijianming'])):
                        lists1.append({'chufacishu': ob[1]['chufacishu'][x], 'shijianming': ob[1]['shijianming'][x],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'shijianming'], as_index=False).agg({'chufacishu': 'max'})
            group_name1 = group_name.groupby(['shijianming'], as_index=False).agg({'chufacishu': 'mean'})
            lists = group_name1.to_dict(orient='list')
            lists['chufacishu'] = np.around((np.array(lists['chufacishu']) - np.array(lists['chufacishu']).mean()),
                                            decimals=2).tolist()
            # print(lists)
        elif (request.GET['type'] == 'yitianxiaoshiyonghuqushi'):
            start_date = parse(request.GET['riqi']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['riqi']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Yonghufenxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                         "yonghuqushi")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and ("laoyonghu" in ob[1]):
                        for x in range(len(ob[1]['laoyonghu'])):
                            lists1.append({'laoyonghu': ob[1]['laoyonghu'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                           'xinyonghu': ob[1]['xinyonghu'][x], 'qidongyonghu': ob[1]['qidongyonghu'][x],
                                           'banbenlaoyonghu': ob[1]['banbenlaoyonghu'][x],
                                           'shijian': ob[0].strftime('%H')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian'], as_index=False).max(
                ['laoyonghu', 'xinyonghu', 'qidongcishu', 'qidongyonghu', 'banbenlaoyonghu'])
            lists = group_name.to_dict(orient='list')
        elif (request.GET['type'] == 'yonghuqushi'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Yonghufenxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                         "yonghuqushi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("laoyonghu" in ob[1]):
                    for x in range(len(ob[1]['laoyonghu'])):
                        lists1.append({'laoyonghu': ob[1]['laoyonghu'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                       'xinyonghu': ob[1]['xinyonghu'][x], 'qidongyonghu': ob[1]['qidongyonghu'][x],
                                       'banbenlaoyonghu': ob[1]['banbenlaoyonghu'][x],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian'], as_index=False).max(
                ['laoyonghu', 'xinyonghu', 'qidongcishu', 'qidongyonghu', 'banbenlaoyonghu'])
            lists = group_name.to_dict(orient='list')
            # print(lists)
        elif (request.GET['type'] == 'nianyonghuqushi'):
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear).values_list("shijian", "yonghuqushi")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and ("laoyonghu" in ob[1]):
                        for x in range(len(ob[1]['laoyonghu'])):
                            lists1.append({'laoyonghu': ob[1]['laoyonghu'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                           'xinyonghu': ob[1]['xinyonghu'][x], 'qidongyonghu': ob[1]['qidongyonghu'][x],
                                           'banbenlaoyonghu': ob[1]['banbenlaoyonghu'][x],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian'], as_index=False).max(
                ['laoyonghu', 'xinyonghu', 'qidongcishu', 'qidongyonghu', 'banbenlaoyonghu'])
            shijian_id = group_name.pop('shijian')  # // shijian获取索列
            # // shijian重新取值，取索列第一位字符
            shijian_id = shijian_id.str[:7]
            # // 在第二列新增shijian列并用shijian（第一位字符）填充
            group_name.insert(0, 'shijian', shijian_id)
            group_name1 = group_name.groupby(['shijian'], as_index=False).agg(
                {'laoyonghu': qshuming, 'xinyonghu': qshuming, 'qidongcishu': qshuming, 'qidongyonghu': qshuming,
                 'banbenlaoyonghu': qshuming})
            lists = group_name1.to_dict(orient='list')
        elif (request.GET['type'] == 'quanbuyonghuqushi'):
            qshuming = request.GET['shuming']
            ulist = models.Yonghufenxi.objects.values_list("shijian", "yonghuqushi")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and ("laoyonghu" in ob[1]):
                        for x in range(len(ob[1]['laoyonghu'])):
                            lists1.append({'laoyonghu': ob[1]['laoyonghu'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                           'xinyonghu': ob[1]['xinyonghu'][x], 'qidongyonghu': ob[1]['qidongyonghu'][x],
                                           'banbenlaoyonghu': ob[1]['banbenlaoyonghu'][x],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian'], as_index=False).max(
                ['laoyonghu', 'xinyonghu', 'qidongcishu', 'qidongyonghu', 'banbenlaoyonghu'])
            shijian_id = group_name.pop('shijian')  # // shijian获取索列
            # // shijian重新取值，取索列第一位字符
            shijian_id = shijian_id.str[:4]
            # // 在第二列新增shijian列并用shijian（第一位字符）填充
            group_name.insert(0, 'shijian', shijian_id)
            group_name1 = group_name.groupby(['shijian'], as_index=False).agg(
                {'laoyonghu': qshuming, 'xinyonghu': qshuming, 'qidongcishu': qshuming, 'qidongyonghu': qshuming,
                 'banbenlaoyonghu': qshuming})
            lists = group_name1.to_dict(orient='list')
        elif (request.GET['type'] == 'nianriliyonghuqushi'):
            qyear = request.GET['year']
            qleiming = request.GET['leiming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear).values_list("shijian", "yonghuqushi")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and (qleiming in ob[1]):
                        for x in range(len(ob[1][qleiming])):
                            lists1.append({qleiming: ob[1][qleiming][x], 'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian'], as_index=False).max([qleiming])
            lists1 = group_name.to_dict(orient='list')
            lists = {}
            lists['shuju'] = []
            lists['max'] = int(np.max(lists1[qleiming]))
            lists['min'] = int(np.min(lists1[qleiming]))
            for x2 in range(len(lists1['shijian'])):
                lists['shuju'].append([lists1['shijian'][x2], lists1[qleiming][x2]])
        elif (request.GET['type'] == 'yitianxiaoshihuoyueyonghu'):
            start_date = parse(request.GET['riqi']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['riqi']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Yonghufenxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                         "huoyueyonghu")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("qidongyonghu" in ob[1]):
                    for x in range(len(ob[1]['qidongyonghu'])):
                        lists1.append(
                            {'qidongyonghu': ob[1]['qidongyonghu'][x], 'zhouhuoyueyonghu': ob[1]['zhouhuoyueyonghu'][x],
                             'yuechenmoyonghu': ob[1]['yuechenmoyonghu'][x],
                             'yuehuoyueyonghu': ob[1]['yuehuoyueyonghu'][x],
                             'leijiqidongyonghu': ob[1]['leijiqidongyonghu'][x],
                             'liushiyonghu': ob[1]['liushiyonghu'][x],
                             'shijian': ob[0].strftime('%H')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian'], as_index=False).max(
                ['qidongyonghu', 'zhouhuoyueyonghu', 'yuehuoyueyonghu', 'yuechenmoyonghu', 'leijiqidongyonghu',
                 'liushiyonghu'])
            lists = group_name.to_dict(orient='list')
        elif (request.GET['type'] == 'huoyueyonghu'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            ulist = models.Yonghufenxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                         "huoyueyonghu")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("qidongyonghu" in ob[1]):
                    for x in range(len(ob[1]['qidongyonghu'])):
                        lists1.append(
                            {'qidongyonghu': ob[1]['qidongyonghu'][x], 'zhouhuoyueyonghu': ob[1]['zhouhuoyueyonghu'][x],
                             'yuechenmoyonghu': ob[1]['yuechenmoyonghu'][x],
                             'yuehuoyueyonghu': ob[1]['yuehuoyueyonghu'][x],
                             'leijiqidongyonghu': ob[1]['leijiqidongyonghu'][x],
                             'liushiyonghu': ob[1]['liushiyonghu'][x],
                             'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian'], as_index=False).max(
                ['qidongyonghu', 'zhouhuoyueyonghu', 'yuehuoyueyonghu', 'yuechenmoyonghu', 'leijiqidongyonghu',
                 'liushiyonghu'])
            lists = group_name.to_dict(orient='list')
        elif (request.GET['type'] == 'nianhuoyueyonghu'):
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear).values_list("shijian", "huoyueyonghu")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("qidongyonghu" in ob[1]):
                    for x in range(len(ob[1]['qidongyonghu'])):
                        lists1.append(
                            {'qidongyonghu': ob[1]['qidongyonghu'][x], 'zhouhuoyueyonghu': ob[1]['zhouhuoyueyonghu'][x],
                             'yuechenmoyonghu': ob[1]['yuechenmoyonghu'][x],
                             'yuehuoyueyonghu': ob[1]['yuehuoyueyonghu'][x],
                             'leijiqidongyonghu': ob[1]['leijiqidongyonghu'][x],
                             'liushiyonghu': ob[1]['liushiyonghu'][x],
                             'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian'], as_index=False).max(
                ['qidongyonghu', 'zhouhuoyueyonghu', 'yuehuoyueyonghu', 'yuechenmoyonghu', 'leijiqidongyonghu',
                 'liushiyonghu'])
            shijian_id = group_name.pop('shijian')  # // shijian获取索列
            # // shijian重新取值，取索列第一位字符
            shijian_id = shijian_id.str[:7]
            # // 在第二列新增shijian列并用shijian（第一位字符）填充
            group_name.insert(0, 'shijian', shijian_id)
            group_name1 = group_name.groupby(['shijian'], as_index=False).agg(
                {'qidongyonghu': qshuming, 'zhouhuoyueyonghu': qshuming, 'yuehuoyueyonghu': qshuming,
                 'yuechenmoyonghu': qshuming,
                 'leijiqidongyonghu': qshuming, 'liushiyonghu': qshuming})
            lists = group_name1.to_dict(orient='list')
        elif (request.GET['type'] == 'zhuanhuanming'):
            ulist = models.Zhuanhuafenxi.objects.last()
            lists1 = []
            for x in range(len(ulist.zhuanhualoudou['result'])):
                lists1.append(ulist.zhuanhualoudou['result'][x]['name'])
            lists2 = utils.list_dict_duplicate_removal(lists1)
            for xew in range(len(lists2)):
                lists.append({"value": lists2[xew], "label": lists2[xew]})
        elif (request.GET['type'] == 'riqiduanlujingzhuanhua'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            qzhuanleiming = request.GET['zhuanleiming']
            ulist1 = models.Zhuanhuafenxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                            "lujingzhuanhua")
            ulist2 = models.Zhuanhuafenxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                            "qudaozhuanhua")
            ulist = itertools.chain(ulist1, ulist2)
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("result" in ob[1]):
                    for x in range(len(ob[1]['result'])):
                        for x1 in range(len(ob[1]['result'][x]['stepList'])):
                            lists1.append({'zhuanhuaming': ob[1]['result'][x]['name'],
                                           'name': ob[1]['result'][x]['stepList'][x1]['name'],
                                           'value': ob[1]['result'][x]['stepList'][x1]['value'],
                                           'order': ob[1]['result'][x]['stepList'][x1]['order'],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            df2 = df1.set_index('zhuanhuaming', inplace=False, drop=False)
            df1 = (df2.loc[qzhuanleiming, ['zhuanhuaming', 'name', 'value', 'order', 'shijian']]).reset_index(drop=True)
            group_name = df1.groupby(['shijian', 'zhuanhuaming', 'name', 'order'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['zhuanhuaming', 'name', 'order'], as_index=False).agg({'value': 'mean'})
            df3 = group_name1.sort_values('order', ascending=True)
            lists2 = df3.to_dict(orient='list')
            lists = {}
            lists['shuju'] = []
            lists['ming'] = []
            for x2 in range(len(lists2['name'])):
                lists['shuju'].append({'name': lists2['name'][x2], 'value': lists2['value'][x2]})
                lists['ming'].append(lists2['name'][x2])
        elif (request.GET['type'] == 'nianyuelujingzhuanhua'):
            qyear = request.GET['year']
            qmonth = request.GET['month']
            qzhuanleiming = request.GET['zhuanleiming']
            ulist1 = models.Zhuanhuafenxi.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list(
                "shijian", "lujingzhuanhua")
            ulist2 = models.Zhuanhuafenxi.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list(
                "shijian", "qudaozhuanhua")
            ulist = itertools.chain(ulist1, ulist2)
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("result" in ob[1]):
                    for x in range(len(ob[1]['result'])):
                        for x1 in range(len(ob[1]['result'][x]['stepList'])):
                            lists1.append({'zhuanhuaming': ob[1]['result'][x]['name'],
                                           'name': ob[1]['result'][x]['stepList'][x1]['name'],
                                           'value': ob[1]['result'][x]['stepList'][x1]['value'],
                                           'order': ob[1]['result'][x]['stepList'][x1]['order'],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            df2 = df1.set_index('zhuanhuaming', inplace=False, drop=False)
            df1 = (df2.loc[qzhuanleiming, ['zhuanhuaming', 'name', 'value', 'order', 'shijian']]).reset_index(drop=True)
            group_name = df1.groupby(['shijian', 'zhuanhuaming', 'name', 'order'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['zhuanhuaming', 'name', 'order'], as_index=False).agg({'value': 'mean'})
            df3 = group_name1.sort_values('order', ascending=True)
            lists2 = df3.to_dict(orient='list')
            lists = {}
            lists['shuju'] = []
            lists['ming'] = []
            for x2 in range(len(lists2['name'])):
                lists['shuju'].append({'name': lists2['name'][x2], 'value': lists2['value'][x2]})
                lists['ming'].append(lists2['name'][x2])
        elif (request.GET['type'] == 'nianlujingzhuanhua'):
            qyear = request.GET['year']
            qzhuanleiming = request.GET['zhuanleiming']
            ulist1 = models.Zhuanhuafenxi.objects.filter(shijian__year=qyear).values_list("shijian", "lujingzhuanhua")
            ulist2 = models.Zhuanhuafenxi.objects.filter(shijian__year=qyear).values_list("shijian", "qudaozhuanhua")
            ulist = itertools.chain(ulist1, ulist2)
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("result" in ob[1]):
                    for x in range(len(ob[1]['result'])):
                        for x1 in range(len(ob[1]['result'][x]['stepList'])):
                            lists1.append({'zhuanhuaming': ob[1]['result'][x]['name'],
                                           'name': ob[1]['result'][x]['stepList'][x1]['name'],
                                           'value': ob[1]['result'][x]['stepList'][x1]['value'],
                                           'order': ob[1]['result'][x]['stepList'][x1]['order'],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            df2 = df1.set_index('zhuanhuaming', inplace=False, drop=False)
            df1 = (df2.loc[qzhuanleiming, ['zhuanhuaming', 'name', 'value', 'order', 'shijian']]).reset_index(drop=True)
            group_name = df1.groupby(['shijian', 'zhuanhuaming', 'name', 'order'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['zhuanhuaming', 'name', 'order'], as_index=False).agg({'value': 'mean'})
            df3 = group_name1.sort_values('order', ascending=True)
            lists2 = df3.to_dict(orient='list')
            lists = {}
            lists['shuju'] = []
            lists['ming'] = []
            for x2 in range(len(lists2['name'])):
                lists['shuju'].append({'name': lists2['name'][x2], 'value': lists2['value'][x2]})
                lists['ming'].append(lists2['name'][x2])
        elif (request.GET['type'] == 'yuediyufenbu'):
            qyear = request.GET['year']
            qmonth = request.GET['month']
            qshuming = request.GET['shuming']
            qleiming = request.GET['leiming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list("shijian",
                                                                                                              "diyufenbu")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and (qleiming in ob[1]):
                        for x in range(len(ob[1]['diqu'])):
                            lists1.append({'name': ob[1]['diqu'][x], 'value': ob[1][qleiming][x],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            # print(df1)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['name'], as_index=False).agg({'value': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['shuju'] = []
            lists['max'] = int(np.max(lists2['value']))
            lists['min'] = int(np.min(lists2['value']))
            for x2 in range(len(lists2['name'])):
                lists['shuju'].append({'name': lists2['name'][x2], 'value': lists2['value'][x2]})
        elif (request.GET['type'] == 'niandiyufenbu'):
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            qleiming = request.GET['leiming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear).values_list("shijian", "diyufenbu")
            lists1 = []
            if (len(ulist) > 0):
                for ob in ulist:
                    if (ob[1] != None) and (qleiming in ob[1]):
                        for x in range(len(ob[1]['diqu'])):
                            lists1.append({'name': ob[1]['diqu'][x], 'value': ob[1][qleiming][x],
                                           'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            # print(df1)
            group_name = df1.groupby(['shijian', 'name'], as_index=False).agg({'value': 'max'})
            group_name1 = group_name.groupby(['name'], as_index=False).agg({'value': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['shuju'] = []
            lists['max'] = int(np.max(lists2['value']))
            lists['min'] = int(np.min(lists2['value']))
            for x2 in range(len(lists2['name'])):
                lists['shuju'].append({'name': lists2['name'][x2], 'value': lists2['value'][x2]})
        elif (request.GET['type'] == 'banbenfenxi'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            qshuming = request.GET['shuming']
            ulist = models.Yonghufenxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                         "banbenfenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("banben" in ob[1]):
                    for x in range(len(ob[1]['banben'])):
                        lists1.append({'banben': ob[1]['banben'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                       'xinyonghu': ob[1]['xinyonghu'][x], 'qidongyonghu': ob[1]['qidongyonghu'][x],
                                       'pingjunshiyongshichang': ob[1]['pingjunshiyongshichang'][x],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'banben'], as_index=False).agg(
                {'xinyonghu': 'max', 'qidongcishu': 'max', 'qidongyonghu': 'max', 'pingjunshiyongshichang': 'max'})
            group_name1 = group_name.groupby(['banben'], as_index=False).agg(
                {'xinyonghu': qshuming, 'qidongcishu': qshuming, 'qidongyonghu': qshuming,
                 'pingjunshiyongshichang': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['xinyonghu'] = []
            lists['qidongcishu'] = []
            lists['qidongyonghu'] = []
            lists['pingjunshiyongshichang'] = []
            for x2 in range(len(lists2['banben'])):
                lists['xinyonghu'].append({'name': lists2['banben'][x2], 'value': lists2['xinyonghu'][x2]})
                lists['qidongcishu'].append({'name': lists2['banben'][x2], 'value': lists2['qidongcishu'][x2]})
                lists['qidongyonghu'].append({'name': lists2['banben'][x2], 'value': lists2['qidongyonghu'][x2]})
                lists['pingjunshiyongshichang'].append(
                    {'name': lists2['banben'][x2], 'value': lists2['pingjunshiyongshichang'][x2]})
            # print(lists)
        elif (request.GET['type'] == 'yuebanbenfenxi'):
            qyear = request.GET['year']
            qmonth = request.GET['month']
            qshuming = request.GET['shuming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list("shijian",
                                                                                                              "banbenfenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("banben" in ob[1]):
                    for x in range(len(ob[1]['banben'])):
                        lists1.append({'banben': ob[1]['banben'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                       'xinyonghu': ob[1]['xinyonghu'][x], 'qidongyonghu': ob[1]['qidongyonghu'][x],
                                       'pingjunshiyongshichang': ob[1]['pingjunshiyongshichang'][x],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'banben'], as_index=False).agg(
                {'xinyonghu': 'max', 'qidongcishu': 'max', 'qidongyonghu': 'max', 'pingjunshiyongshichang': 'max'})
            group_name1 = group_name.groupby(['banben'], as_index=False).agg(
                {'xinyonghu': qshuming, 'qidongcishu': qshuming, 'qidongyonghu': qshuming,
                 'pingjunshiyongshichang': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['xinyonghu'] = []
            lists['qidongcishu'] = []
            lists['qidongyonghu'] = []
            lists['pingjunshiyongshichang'] = []
            for x2 in range(len(lists2['banben'])):
                lists['xinyonghu'].append({'name': lists2['banben'][x2], 'value': lists2['xinyonghu'][x2]})
                lists['qidongcishu'].append({'name': lists2['banben'][x2], 'value': lists2['qidongcishu'][x2]})
                lists['qidongyonghu'].append({'name': lists2['banben'][x2], 'value': lists2['qidongyonghu'][x2]})
                lists['pingjunshiyongshichang'].append(
                    {'name': lists2['banben'][x2], 'value': lists2['pingjunshiyongshichang'][x2]})
            # print(lists)
        elif (request.GET['type'] == 'nianbanbenfenxi'):
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Yonghufenxi.objects.filter(shijian__year=qyear).values_list("shijian", "banbenfenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("banben" in ob[1]):
                    for x in range(len(ob[1]['banben'])):
                        lists1.append({'banben': ob[1]['banben'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                       'xinyonghu': ob[1]['xinyonghu'][x], 'qidongyonghu': ob[1]['qidongyonghu'][x],
                                       'pingjunshiyongshichang': ob[1]['pingjunshiyongshichang'][x],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'banben'], as_index=False).agg(
                {'xinyonghu': 'max', 'qidongcishu': 'max', 'qidongyonghu': 'max', 'pingjunshiyongshichang': 'max'})
            group_name1 = group_name.groupby(['banben'], as_index=False).agg(
                {'xinyonghu': qshuming, 'qidongcishu': qshuming, 'qidongyonghu': qshuming,
                 'pingjunshiyongshichang': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['xinyonghu'] = []
            lists['qidongcishu'] = []
            lists['qidongyonghu'] = []
            lists['pingjunshiyongshichang'] = []
            for x2 in range(len(lists2['banben'])):
                lists['xinyonghu'].append({'name': lists2['banben'][x2], 'value': lists2['xinyonghu'][x2]})
                lists['qidongcishu'].append({'name': lists2['banben'][x2], 'value': lists2['qidongcishu'][x2]})
                lists['qidongyonghu'].append({'name': lists2['banben'][x2], 'value': lists2['qidongyonghu'][x2]})
                lists['pingjunshiyongshichang'].append(
                    {'name': lists2['banben'][x2], 'value': lists2['pingjunshiyongshichang'][x2]})
            # print(lists)
        elif (request.GET['type'] == 'qudaofenxi'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            qshuming = request.GET['shuming']
            ulist = models.Qudaoxinxi.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                        "qudaofenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("qudaoming" in ob[1]):
                    for x in range(len(ob[1]['qudaoming'])):
                        lists1.append({'qudaoming': ob[1]['qudaoming'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                       'qidongyonghu': ob[1]['qidongyonghu'][x],
                                       'qudaoxinyonghu': ob[1]['qudaoxinyonghu'][x],
                                       'benqudaoxinyonghu': ob[1]['benqudaoxinyonghu'][x],
                                       'pingjunsyshichang': ob[1]['pingjunsyshichang'][x],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'qudaoming'], as_index=False).agg(
                {'qidongcishu': 'max', 'qidongyonghu': 'max', 'qudaoxinyonghu': 'max', 'benqudaoxinyonghu': 'max',
                 'pingjunsyshichang': 'max'})
            group_name1 = group_name.groupby(['qudaoming'], as_index=False).agg(
                {'qidongcishu': qshuming, 'qidongyonghu': qshuming, 'qudaoxinyonghu': qshuming,
                 'benqudaoxinyonghu': qshuming, 'pingjunsyshichang': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['qidongcishu'] = []
            lists['qidongyonghu'] = []
            lists['qudaoxinyonghu'] = []
            lists['benqudaoxinyonghu'] = []
            lists['pingjunsyshichang'] = []
            for x2 in range(len(lists2['qudaoming'])):
                lists['qidongcishu'].append({'name': lists2['qudaoming'][x2], 'value': lists2['qidongcishu'][x2]})
                lists['qidongyonghu'].append({'name': lists2['qudaoming'][x2], 'value': lists2['qidongyonghu'][x2]})
                lists['qudaoxinyonghu'].append({'name': lists2['qudaoming'][x2], 'value': lists2['qudaoxinyonghu'][x2]})
                lists['benqudaoxinyonghu'].append(
                    {'name': lists2['qudaoming'][x2], 'value': lists2['benqudaoxinyonghu'][x2]})
                lists['pingjunsyshichang'].append(
                    {'name': lists2['qudaoming'][x2], 'value': lists2['pingjunsyshichang'][x2]})
            # print(lists)
        elif (request.GET['type'] == 'yuequdaofenxi'):
            qyear = request.GET['year']
            qmonth = request.GET['month']
            qshuming = request.GET['shuming']
            ulist = models.Qudaoxinxi.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list("shijian",
                                                                                                             "qudaofenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("qudaoming" in ob[1]):
                    for x in range(len(ob[1]['qudaoming'])):
                        lists1.append({'qudaoming': ob[1]['qudaoming'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                       'qidongyonghu': ob[1]['qidongyonghu'][x],
                                       'qudaoxinyonghu': ob[1]['qudaoxinyonghu'][x],
                                       'benqudaoxinyonghu': ob[1]['benqudaoxinyonghu'][x],
                                       'pingjunsyshichang': ob[1]['pingjunsyshichang'][x],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'qudaoming'], as_index=False).agg(
                {'qidongcishu': 'max', 'qidongyonghu': 'max', 'qudaoxinyonghu': 'max', 'benqudaoxinyonghu': 'max',
                 'pingjunsyshichang': 'max'})
            group_name1 = group_name.groupby(['qudaoming'], as_index=False).agg(
                {'qidongcishu': qshuming, 'qidongyonghu': qshuming, 'qudaoxinyonghu': qshuming,
                 'benqudaoxinyonghu': qshuming, 'pingjunsyshichang': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['qidongcishu'] = []
            lists['qidongyonghu'] = []
            lists['qudaoxinyonghu'] = []
            lists['benqudaoxinyonghu'] = []
            lists['pingjunsyshichang'] = []
            for x2 in range(len(lists2['qudaoming'])):
                lists['qidongcishu'].append({'name': lists2['qudaoming'][x2], 'value': lists2['qidongcishu'][x2]})
                lists['qidongyonghu'].append({'name': lists2['qudaoming'][x2], 'value': lists2['qidongyonghu'][x2]})
                lists['qudaoxinyonghu'].append({'name': lists2['qudaoming'][x2], 'value': lists2['qudaoxinyonghu'][x2]})
                lists['benqudaoxinyonghu'].append(
                    {'name': lists2['qudaoming'][x2], 'value': lists2['benqudaoxinyonghu'][x2]})
                lists['pingjunsyshichang'].append(
                    {'name': lists2['qudaoming'][x2], 'value': lists2['pingjunsyshichang'][x2]})
            # print(lists)
        elif (request.GET['type'] == 'nianqudaofenxi'):
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Qudaoxinxi.objects.filter(shijian__year=qyear).values_list("shijian", "qudaofenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("qudaoming" in ob[1]):
                    for x in range(len(ob[1]['qudaoming'])):
                        lists1.append({'qudaoming': ob[1]['qudaoming'][x], 'qidongcishu': ob[1]['qidongcishu'][x],
                                       'qidongyonghu': ob[1]['qidongyonghu'][x],
                                       'qudaoxinyonghu': ob[1]['qudaoxinyonghu'][x],
                                       'benqudaoxinyonghu': ob[1]['benqudaoxinyonghu'][x],
                                       'pingjunsyshichang': ob[1]['pingjunsyshichang'][x],
                                       'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'qudaoming'], as_index=False).agg(
                {'qidongcishu': 'max', 'qidongyonghu': 'max', 'qudaoxinyonghu': 'max', 'benqudaoxinyonghu': 'max',
                 'pingjunsyshichang': 'max'})
            group_name1 = group_name.groupby(['qudaoming'], as_index=False).agg(
                {'qidongcishu': qshuming, 'qidongyonghu': qshuming, 'qudaoxinyonghu': qshuming,
                 'benqudaoxinyonghu': qshuming, 'pingjunsyshichang': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['qidongcishu'] = []
            lists['qidongyonghu'] = []
            lists['qudaoxinyonghu'] = []
            lists['benqudaoxinyonghu'] = []
            lists['pingjunsyshichang'] = []
            for x2 in range(len(lists2['qudaoming'])):
                lists['qidongcishu'].append({'name': lists2['qudaoming'][x2], 'value': lists2['qidongcishu'][x2]})
                lists['qidongyonghu'].append({'name': lists2['qudaoming'][x2], 'value': lists2['qidongyonghu'][x2]})
                lists['qudaoxinyonghu'].append({'name': lists2['qudaoming'][x2], 'value': lists2['qudaoxinyonghu'][x2]})
                lists['benqudaoxinyonghu'].append(
                    {'name': lists2['qudaoming'][x2], 'value': lists2['benqudaoxinyonghu'][x2]})
                lists['pingjunsyshichang'].append(
                    {'name': lists2['qudaoming'][x2], 'value': lists2['pingjunsyshichang'][x2]})
            # print(lists)
        elif (request.GET['type'] == 'yemianfenxi'):
            start_date = parse(request.GET['start_date']).strftime('%Y-%m-%d 00:00:00')
            end_date = parse(request.GET['end_date']).strftime('%Y-%m-%d 23:59:59')
            qshuming = request.GET['shuming']
            ulist = models.Shiyongxingwei.objects.filter(shijian__range=(start_date, end_date)).values_list("shijian",
                                                                                                            "yemianfenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("yemianmingcheng" in ob[1]):
                    for x in range(len(ob[1]['yemianmingcheng'])):
                        lists1.append(
                            {'yemianmingcheng': ob[1]['yemianmingcheng'][x], 'fangwencishu': ob[1]['fangwencishu'][x],
                             'yemiantiaochulv': ob[1]['yemiantiaochulv'][x],
                             'pingjvtinngliushijian': ob[1]['pingjvtinngliushijian'][x],
                             'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'yemianmingcheng'], as_index=False).agg(
                {'fangwencishu': 'max', 'yemiantiaochulv': 'max',
                 'pingjvtinngliushijian': 'max'})
            group_name1 = group_name.groupby(['yemianmingcheng'], as_index=False).agg(
                {'fangwencishu': qshuming, 'yemiantiaochulv': qshuming,
                 'pingjvtinngliushijian': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['fangwencishu'] = []
            lists['yemiantiaochulv'] = []
            lists['pingjvtinngliushijian'] = []
            for x2 in range(len(lists2['yemianmingcheng'])):
                lists['fangwencishu'].append(
                    {'name': lists2['yemianmingcheng'][x2], 'value': lists2['fangwencishu'][x2]})
                lists['yemiantiaochulv'].append(
                    {'name': lists2['yemianmingcheng'][x2], 'value': lists2['yemiantiaochulv'][x2]})
                lists['pingjvtinngliushijian'].append(
                    {'name': lists2['yemianmingcheng'][x2], 'value': lists2['pingjvtinngliushijian'][x2]})
            # print(lists)
        elif (request.GET['type'] == 'yueyemianfenxi'):
            qyear = request.GET['year']
            qmonth = request.GET['month']
            qshuming = request.GET['shuming']
            ulist = models.Shiyongxingwei.objects.filter(shijian__year=qyear, shijian__month=qmonth).values_list(
                "shijian", "yemianfenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("yemianmingcheng" in ob[1]):
                    for x in range(len(ob[1]['yemianmingcheng'])):
                        lists1.append(
                            {'yemianmingcheng': ob[1]['yemianmingcheng'][x], 'fangwencishu': ob[1]['fangwencishu'][x],
                             'yemiantiaochulv': ob[1]['yemiantiaochulv'][x],
                             'pingjvtinngliushijian': ob[1]['pingjvtinngliushijian'][x],
                             'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'yemianmingcheng'], as_index=False).agg(
                {'fangwencishu': 'max', 'yemiantiaochulv': 'max',
                 'pingjvtinngliushijian': 'max'})
            group_name1 = group_name.groupby(['yemianmingcheng'], as_index=False).agg(
                {'fangwencishu': qshuming, 'yemiantiaochulv': qshuming,
                 'pingjvtinngliushijian': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['fangwencishu'] = []
            lists['yemiantiaochulv'] = []
            lists['pingjvtinngliushijian'] = []
            for x2 in range(len(lists2['yemianmingcheng'])):
                lists['fangwencishu'].append(
                    {'name': lists2['yemianmingcheng'][x2], 'value': lists2['fangwencishu'][x2]})
                lists['yemiantiaochulv'].append(
                    {'name': lists2['yemianmingcheng'][x2], 'value': lists2['yemiantiaochulv'][x2]})
                lists['pingjvtinngliushijian'].append(
                    {'name': lists2['yemianmingcheng'][x2], 'value': lists2['pingjvtinngliushijian'][x2]})
            # print(lists)
        elif (request.GET['type'] == 'nianyemianfenxi'):
            qyear = request.GET['year']
            qshuming = request.GET['shuming']
            ulist = models.Shiyongxingwei.objects.filter(shijian__year=qyear).values_list("shijian", "yemianfenxi")
            lists1 = []
            for ob in ulist:
                if (ob[1] != None) and ("yemianmingcheng" in ob[1]):
                    for x in range(len(ob[1]['yemianmingcheng'])):
                        lists1.append(
                            {'yemianmingcheng': ob[1]['yemianmingcheng'][x], 'fangwencishu': ob[1]['fangwencishu'][x],
                             'yemiantiaochulv': ob[1]['yemiantiaochulv'][x],
                             'pingjvtinngliushijian': ob[1]['pingjvtinngliushijian'][x],
                             'shijian': ob[0].strftime('%Y-%m-%d')})
            df1 = pd.DataFrame(lists1)
            group_name = df1.groupby(['shijian', 'yemianmingcheng'], as_index=False).agg(
                {'fangwencishu': 'max', 'yemiantiaochulv': 'max',
                 'pingjvtinngliushijian': 'max'})
            group_name1 = group_name.groupby(['yemianmingcheng'], as_index=False).agg(
                {'fangwencishu': qshuming, 'yemiantiaochulv': qshuming,
                 'pingjvtinngliushijian': qshuming})
            lists2 = group_name1.to_dict(orient='list')
            lists = {}
            lists['fangwencishu'] = []
            lists['yemiantiaochulv'] = []
            lists['pingjvtinngliushijian'] = []
            for x2 in range(len(lists2['yemianmingcheng'])):
                lists['fangwencishu'].append(
                    {'name': lists2['yemianmingcheng'][x2], 'value': lists2['fangwencishu'][x2]})
                lists['yemiantiaochulv'].append(
                    {'name': lists2['yemianmingcheng'][x2], 'value': lists2['yemiantiaochulv'][x2]})
                lists['pingjvtinngliushijian'].append(
                    {'name': lists2['yemianmingcheng'][x2], 'value': lists2['pingjvtinngliushijian'][x2]})
            # print(lists)

    return JsonResponse({"data": lists})


def login(request):
    if request.method == "GET":
        return render(request, "First/登录.html")
    username = request.POST.get("username")
    password = request.POST.get("password")
    user_obj = auth.authenticate(username=username, password=password)
    if not user_obj:
        return JsonResponse({"res": "账号或密码错误"})
    else:
        auth.login(request, user_obj)
        path = request.GET.get("next") or "/index/"
        return JsonResponse({"res": path})


@login_required
def logout(request):
    ppp = auth.logout(request)
    return JsonResponse({"res": "/login/"})


@login_required
def index(request):
    username = request.user.username
    return render(request, "First/首页.html", {"username": username})


@login_required
def qbzhongduanfenxi(request):
    ulist = models.Yonghufenxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/终端分析.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbshijianfenxi(request):
    ulist = models.Shijianxinxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/事件分析.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbbanbenfenxi(request):
    ulist = models.Yonghufenxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/版本分析.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbliucunfenxi(request):
    ulist = models.Liucunfenxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/留存分析.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbyonghuhuaxiang(request):
    ulist = models.Yonghufenxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/用户画像.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbqudaofenxi(request):
    ulist = models.Qudaoxinxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/渠道分析.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbzhuanhuafenxi(request):
    ulist = models.Zhuanhuafenxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/转化分析.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbhuoyueyonghu(request):
    ulist = models.Yonghufenxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/活跃用户.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbyemianfenxi(request):
    ulist = models.Shiyongxingwei.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/页面分析.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbyonghuqushi(request):
    ulist = models.Yonghufenxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/用户趋势.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbdiyufenbu(request):
    ulist = models.Yonghufenxi.objects.last()
    start_date = utils.get_qian_time(ulist.shijian, "6天前")
    start_date = str(parse(start_date).strftime('%Y-%m-%d'))
    end_date = str((ulist.shijian).strftime('%Y-%m-%d'))
    year = str((ulist.shijian).strftime('%Y'))
    month = str((ulist.shijian).strftime('%m'))
    return render(request, "First/地域分布.html",
                  {"start_date": start_date, "end_date": end_date, "year": year, "month": month})


@login_required
def qbrenwuguanli(request):
    if request.method == "GET":
        return render(request, "First/任务管理.html")
    if request.POST.get("qingqiuming") == "mouzanting":
        scheduler.pause_job(request.POST.get("renwuid"))
        return JsonResponse({"res": "暂停成功"})
    elif request.POST.get("qingqiuming") == "mouhuifu":
        scheduler.resume_job(request.POST.get("renwuid"))
        return JsonResponse({"res": "恢复成功"})
    elif request.POST.get("qingqiuming") == "gengxinshujujieru":
        models.Yonghujiapi.objects.create(shijian=(datetime.datetime.now()).strftime('%Y-%m-%d %H:%M:%S'),
                                          yonghuyingyong=json.loads(request.POST.get("yonghuyingyong")),
                                          shujujieru=json.loads(request.POST.get("shujujieru")),
                                          baidufuwu=json.loads(request.POST.get("baidufuwu")))
        scheduler.start()  # 启动任务
        return JsonResponse({"res": "提交成功"})


def hanshurwzanting(renwuid):
    scheduler.pause_job(renwuid)
